using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using log4net;

namespace X.Ray.Core.Framework
{
    /// <summary>
    /// Author:					Joe Audette
    /// Created:				2007-04-29
    /// Last Modified:			2007-08-14
    /// 
    /// The use and distribution terms for this software are covered by the 
    /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
    /// which can be found in the file CPL.TXT at the root of this distribution.
    /// By using this software in any fashion, you are agreeing to be bound by 
    /// the terms of this license.
    ///
    /// You must not remove this notice, or any other, from this software.
    /// 
    /// </summary>
    public static class ResourceHelper
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ResourceHelper));

        #region Help System

        public static CultureInfo GetDefaultCulture()
        {
            try
            {
                // reading config sections not allowed in Medium trust so catch SecurityException
                GlobalizationSection globalizationSection = (GlobalizationSection)ConfigurationManager.GetSection("system.web/globalization");
                if (globalizationSection != null)
                {
                    if (globalizationSection.Culture.Contains(":"))
                    {
                        String cultureString;
                        cultureString = globalizationSection.Culture.Substring(globalizationSection.Culture.LastIndexOf(":") + 1, (globalizationSection.Culture.Length - (globalizationSection.Culture.LastIndexOf(":") + 1)));
                        return new CultureInfo(cultureString);
                    }
                    else
                    {
                        return new CultureInfo(globalizationSection.Culture);
                    }
                }
            }
            catch (ArgumentException) { }
            catch (System.Security.SecurityException){ }

            return CultureInfo.CurrentCulture;
        }

        public static string GetHelpFileText(string helpKey)
        {
            if (String.IsNullOrEmpty(helpKey)) return String.Empty;

            string culture = CultureInfo.CurrentCulture.Name;
            string fileExtension = ".config";
            string helpFolder = GetHelpFolder();
            string helpFile;
            if (File.Exists(helpFolder + culture + "-" + helpKey + fileExtension))
            {
                helpFile = helpFolder + culture + "-" + helpKey + fileExtension;
            }
            else
            {
                helpFile = helpFolder + "en-US-" + helpKey + fileExtension;
            }

            string message = String.Empty;
            if (File.Exists(helpFile))
            {
                FileInfo file = new FileInfo(helpFile);
                StreamReader sr = file.OpenText();
                message = sr.ReadToEnd();
                sr.Close();
            }
            return message;
        }

        public static void SetHelpFileText(String helpKey, String helpText)
        {
            if (String.IsNullOrEmpty(helpKey) || String.IsNullOrEmpty(helpText)) return;

            string culture = CultureInfo.CurrentCulture.Name;
            string fileExtension = ".config";
            string helpFolder = GetHelpFolder();
            string helpFile;
            helpFile = helpFolder + culture + "-" + helpKey + fileExtension;

            StreamWriter streamWriter = File.CreateText(helpFile);
            streamWriter.Write(helpText);
            streamWriter.Close();
        }

        public static String GetHelpFolder()
        {
            if (HttpContext.Current == null) return String.Empty;
            return HttpContext.Current.Server.MapPath(WebUtils.GetApplicationRoot()
                    + "/Data/HelpFiles") + Path.DirectorySeparatorChar;
        }

        #endregion

        #region Message Templates

        public static string GetMessageTemplate(string templateFile)
        {
            return GetMessageTemplate(CultureInfo.CurrentCulture, templateFile);
        }

        public static string GetMessageTemplate(CultureInfo cultureInfo, string templateFile)
        {
            if (String.IsNullOrEmpty(templateFile)) return String.Empty;

            string culture = cultureInfo.Name;
            string templateFolder = GetMessageTemplateFolder();
            string messageFile;

            if (File.Exists(templateFolder + culture + "-" + templateFile))
            {
                messageFile = templateFolder + culture + "-" + templateFile;
            }
            else
            {
                messageFile = templateFolder + "en-US-" + templateFile;
            }

            if (File.Exists(messageFile))
            {
                string message = File.ReadAllText(messageFile);
                
                return message;
            }
            else
            {
                return string.Empty;
            }
        }

        //public static void SetMessageTemplate(String templateKey, String templateText)
        //{
        //    if (String.IsNullOrEmpty(templateKey) || String.IsNullOrEmpty(templateText)) return;

        //    String culture = CultureInfo.CurrentCulture.Name;
        //    //String fileExtension = ".config";
        //    if (!templateKey.EndsWith(".config"))
        //    {
        //        templateKey += ".config";
        //    }

        //    string templateFolder = GetMessageTemplateFolder();
        //    string messageFile = templateFolder + culture + "-" + templateKey;

        //    StreamWriter streamWriter = File.CreateText(messageFile);
        //    streamWriter.Write(templateText);
        //    streamWriter.Close();
        //}


        public static String GetMessageTemplateFolder()
        {
            if (HttpContext.Current == null) return String.Empty;
            return HttpContext.Current.Server.MapPath(WebUtils.GetApplicationRoot()
                    + "/Data/MessageTemplates") + Path.DirectorySeparatorChar;
        }


        #endregion

        public static string GetResourceString(
            string resourceFile, 
            string resourceKey)
        {
            if (HttpContext.Current == null) return resourceKey;
            if (resourceFile.Length == 0) resourceFile = "Resource";

            try
            {
                object resource = HttpContext.GetGlobalResourceObject(
                    resourceFile, resourceKey);

                if (resource != null) return resource.ToString();
            }
            catch { }

            return resourceKey;

        }

    }
}
